/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc.mapper;

import cn.yzc.pojo.Depp;
import cn.yzc.pojo.Dept;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>Project: mybatis_pro - DeptMapper</p>
 * <p>Powered by yzc On 2023-10-10 19:51:19</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
public interface DeptMapper {


    List<Dept> queryDept();

    @Select("SELECT id, deptName, parentid FROM dept WHERE parentid = 0")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "deptName", column = "deptName"),
            @Result(property = "parentid", column = "parentid"),
            @Result(property = "deptList", column = "id", javaType = List.class,
                    many = @Many(select = "cn.yzc.mapper.DeptMapper.querySubDepts"))
    })
    List<Dept> queryTopLevelDepts();

    @Select("SELECT id, deptName, parentid FROM dept WHERE parentid = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "deptName", column = "deptName"),
            @Result(property = "parentid", column = "parentid"),
            @Result(property = "deptList", column = "id", javaType = List.class,
                    many = @Many(select = "cn.yzc.mapper.DeptMapper.querySubDepts"))
    })
    List<Dept> querySubDepts(int id);

    @Select(" SELECT d1.id       Aid,\n" +
            "               d1.deptName Aname,\n" +
            "               d1.parentid Aparentid,\n" +
            "               d2.id       Bid,\n" +
            "               d2.deptName Bname,\n" +
            "               d2.parentid Bparentid,\n" +
            "               d3.id       Cid,\n" +
            "               d3.deptName Cname,\n" +
            "               d3.parentid Cparentid\n" +
            "        FROM dept d1\n" +
            "                 LEFT JOIN dept d2 ON d1.id = d2.parentid\n" +
            "                 LEFT JOIN dept d3 ON d2.id = d3.parentid\n" +
            "        WHERE d1.parentid = 0")
    @Results({
            @Result(column = "Aid", property = "Aid"),
            @Result(column = "Aname", property = "Aname"),
            @Result(column = "Aparentid", property = "Aparentid"),
            @Result(column = "Bid", property = "Bid"),
            @Result(column = "Bname", property = "Bname"),
            @Result(column = "Bparentid", property = "Bparentid"),
            @Result(column = "Cid", property = "Cid"),
            @Result(column = "Cname", property = "Cname"),
            @Result(column = "Cparentid", property = "Cparentid"),
            //@Result(column = "Aid", property = "subDept",
            //        many = @Many(select = "querySubDeptByParentId")
            //)
    })
    List<Depp> queryDepp1();


}
